<?php 


$startDate = getWeekStartDate($viewDate);
$endDate = mktime(0, 0, 0, date("m", $startDate), date("d", $startDate)+7, date("Y", $startDate));

$weekDates = array();
for($i=0; $i<7; $i++)
{
	$newDate = mktime(0, 0, 0, date("m", $startDate), date("d", $startDate)+$i, date("Y", $startDate));
	$weekDates[] = array('date' => $newDate);
}

//convert dates to string 2013-03-15 format
// $startDate = date("Y-m-d", $startDate);
// $endDate = date("Y-m-d", $endDate);




echo '<table>';
echo '<tr>';
echo '<th>Employees</th>';
echo '<th><table><tr><td>'. date("l", $weekDates[0]['date']) .'</td><tr><td>'. date("M\nd", $weekDates[0]['date']) .'</td></tr></table></th>';
echo '<th><table><tr><td>'. date("l", $weekDates[1]['date']) .'</td><tr><td>'. date("M\nd", $weekDates[1]['date']) .'</td></tr></table></th>';
echo '<th><table><tr><td>'. date("l", $weekDates[2]['date']) .'</td><tr><td>'. date("M\nd", $weekDates[2]['date']) .'</td></tr></table></th>';
echo '<th><table><tr><td>'. date("l", $weekDates[3]['date']) .'</td><tr><td>'. date("M\nd", $weekDates[3]['date']) .'</td></tr></table></th>';
echo '<th><table><tr><td>'. date("l", $weekDates[4]['date']) .'</td><tr><td>'. date("M\nd", $weekDates[4]['date']) .'</td></tr></table></th>';
echo '<th><table><tr><td>'. date("l", $weekDates[5]['date']) .'</td><tr><td>'. date("M\nd", $weekDates[5]['date']) .'</td></tr></table></th>';
echo '<th><table><tr><td>'. date("l", $weekDates[6]['date']) .'</td><tr><td>'. date("M\nd", $weekDates[6]['date']) .'</td></tr></table></th>';
echo '</tr>';


$getAccountsResult = getAccounts($userName, $userPassHash);
$accounts = null;
//grab the accounts
$accounts = $getAccountsResult['results'];
//parse the accounts
foreach ($accounts as &$accountsEntry) {
	$accName=$accountsEntry['accName'];

	$getAccountResult = getAccount($accName, $userName, $userPassHash);
	$_SESSION[$accName . 'account'] = $getAccountResult['results']['dataHash'];
	getShiftsPerWorker($startDate, $endDate, $accName, $userName, $userPassHash);
}
echo '</table>';

function getShiftsPerWorker($startDate, $endDate, $accName, $userName, $userPassHash)
{
	$checkRangeSchedulePerWorkerResult = checkRangeSchedulePerWorker($userName, $userPassHash, date("Y-m-d", $startDate), date("Y-m-d", $endDate), $accName);
	$shifts = null;
	//grab the shifts
	$shifts = $checkRangeSchedulePerWorkerResult[1];
	//parse the shifts
	echo '<tr>';
	echo '<td>';
	echo $accName;
	echo '</td>';
	for ($i = 0; $i < 7; $i++) {
		$hasShift = false;
		foreach ($shifts as &$shiftsEntry) {
			$shiftDate = $shiftsEntry['datestamp'];
			$shiftDate = strtotime($shiftDate);
			$dayInterval = date("d", $shiftDate) - date("d", $startDate);
			if($dayInterval == $i)
			{
				echo '<td>';
				echo '<form action="template.php">';
				echo '<table>';
				echo '<tr><td>';

				//hidden fields
				echo '<input type="hidden" name="roleID" value="'. $shiftsEntry['roleID'] .'">';
				echo '<input type="hidden" name="dateID" value="'. $shiftsEntry['dateID'] .'">';
				echo '<input type="hidden" name="empID" value="'. $shiftsEntry['empID'] .'">';

				echo '</td>';
				echo '<tr><td>' . $shiftsEntry['roleName'] . '</td>';
				echo '<td>' . $shiftsEntry['startTime'] . '</td></tr>';
					
				echo '<tr><td>' . $shiftsEntry['shiftStatus'] . '</td>';
				echo '<td>' . $shiftsEntry['endTime'] . '</td></tr>';

				echo '<tr><td></td>
						<td>
						<input type="submit" name="shiftAction" value="Delete Shift" />
						</td></tr>';

				echo '</table>';
				echo '</form>';
				echo '</td>';
				$hasShift = true;
			}
		}
		if(!$hasShift)
		{
			echo '<td>';
			echo '</td>';
		}
	}
	echo '</tr>';
}

function getWeekStartDate($viewDate)
{
	$notFound = true;
	$precedingDate = $viewDate;
	while($notFound)
	{
		$strDay = date("l", $precedingDate);
		if($strDay == "Monday")
		{
			$notFound = false;
			break;
		}
		$precedingDate = mktime(0, 0, 0, date("m", $precedingDate), date("d", $precedingDate)-1, date("Y", $precedingDate));
	}
	return $precedingDate;
}


?>